package com.example.csclient.data

import com.example.csclient.network.CscApiService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

interface CscRepository {
    suspend fun registerUser(userName: String, password: String, resultHandler: (String) -> Unit)
    suspend fun subscribeDevice(userName: String, password: String, targetID: String, resultHandler: (String) -> Unit)

    /* 返回是否推送成功 */
    suspend fun publishControlMessage(message: String)

    fun setDefaultHandler(messageHandler: (String) -> Unit)

    fun setRepoInfo(userName: String, password: String)

    /* 尝试重启 Repo */
    fun restartRepository()
}

class NetworkCscRepository(
    private val cscApiService: CscApiService
) : CscRepository {
    private val messageHandler: (String) -> Unit = {}
    private var userName: String = ""
    private var password: String = ""
    private val deviceID: String = "test-term"

    init {
        initRepository()
    }

    private fun initRepository() {
        /* 先注册APP设备到服务器 */
        CoroutineScope(Dispatchers.IO).launch {
            cscApiService.sendMessages(
                "zncdz,devType=TERM,msgType=REG,devID=%s,b=0.0,m=f,t=00:00,w=0,c=0.0,u=0.0"
                    .format(deviceID)
            )
        }
    }

    override fun setDefaultHandler(messageHandler: (String) -> Unit) {
        cscApiService.setMessageHandler(messageHandler)
    }

    override suspend fun registerUser(userName: String, password: String, resultHandler: (String) -> Unit) {
        if (!cscApiService.isConnectedSuccess) {
            resultHandler("Error: 网络不可达")
            restartRepository()
            return
        }
        cscApiService.setMessageHandler(resultHandler)
        cscApiService.sendMessages(
            "zncdz,devType=TERM,msgType=REGU,devID=%s,uname=%s,passwd=%s"
                .format(deviceID, userName, password)
        )
    }

    override suspend fun subscribeDevice(
        userName: String,
        password: String,
        targetID: String,
        resultHandler: (String) -> Unit
    ) {
        if (!cscApiService.isConnectedSuccess) {
            resultHandler("Error: 网络不可达")
            restartRepository()
            return
        }
        cscApiService.setMessageHandler(resultHandler)
        cscApiService.sendMessages(
            "zncdz,devType=TERM,msgType=SUB,devID=%s,uname=%s,passwd=%s,tgtID=%s,param1=mode"
                .format(deviceID, userName, password, targetID)
        )
    }

    override suspend fun publishControlMessage(message: String) {
        if (!cscApiService.isConnectedSuccess) {
            messageHandler("ErrorNetwork")
            return
        }
        cscApiService.sendMessages(
            "zncdz,devType=TERM,msgType=PUB,devID=%s,%s"
                .format(deviceID, message)
        )
    }

    override fun setRepoInfo(userName: String, password: String) {
        this.userName = userName
        this.password = password
    }

    override fun restartRepository() {
        cscApiService.restartService()
        initRepository()
    }
}
